package com.weiyin.examination.activity;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.weiyin.examination.Fragment.HomeFragment;
import com.weiyin.examination.Fragment.PersonalCenterFragment;
import com.weiyin.examination.Fragment.QuestionBankFragment;
import com.weiyin.examination.R;
import com.weiyin.examination.adapter.ViewPagerAdapter;
import com.weiyin.examination.util.StatusUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     e-mail : niejunfeng@win-electr.com
 *     time   : 2018/04/03
 *     desc   :
 *     version: 1.0.0
 *     Copyright: Copyright（c）2017
 *     Company:上海蔚音电子科技有限公司
 * </pre>
 *
 * @author niejunfeng
 */

public class MainActivity extends FragmentActivity implements TabLayout.OnTabSelectedListener, ViewPager.OnPageChangeListener {

    private List<Fragment> mViewList;
    private TabLayout tabLayout;
    private ViewPager viewPager;


    private HomeFragment homeFragment;
    private QuestionBankFragment questionBankFragment;
    private PersonalCenterFragment personalCenterFragment;

    private ViewPagerAdapter viewPagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        StatusUtils.setStatus(this);
        initView();
    }

    private void initTab() {
        for (int i = 0; i < 3; i++) {
            View view = getLayoutInflater().inflate(R.layout.mainactivity_tab_layout, null);
            ImageView icon = (ImageView) view.findViewById(R.id.main_tab_imageView);
            TextView textView = (TextView) view.findViewById(R.id.main_tab_textView);
            if (i == 0) {
                icon.setImageResource(R.drawable.com_ic_home_normal_selector);
                textView.setText("首页");
            } else if (i == 1) {
                icon.setImageResource(R.drawable.com_ic_questuib_normal_selector);
                textView.setText("题库");
            } else if (i == 2) {
                icon.setImageResource(R.drawable.com_ic_my_normal_selector);
                textView.setText("我的");
            }
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setCustomView(view);
            tabLayout.addTab(tab);
        }
        tabLayout.addOnTabSelectedListener(this);
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        initTab();
        mViewList = new ArrayList<>();
        homeFragment = new HomeFragment();
        questionBankFragment = new QuestionBankFragment();
        personalCenterFragment = new PersonalCenterFragment();

        mViewList.add(homeFragment);
        mViewList.add(questionBankFragment);
        mViewList.add(personalCenterFragment);

        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPagerAdapter = new ViewPagerAdapter(fragmentManager);
        viewPagerAdapter.setFragmentList(mViewList);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.addOnPageChangeListener(this);
//        viewPager.setOffscreenPageLimit(4);
        tabLayout.getTabAt(0).select();

    }

    //初始化视图数据
    private void initData() {
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (tab != null) {
            viewPager.setCurrentItem(tab.getPosition());
            tab.getCustomView().setSelected(true);
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        tabLayout.getTabAt(position).select();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}


